<?php

/**
 * Controller_main
 * 维护单位设置
 * 公司名称 简称 人员 联系电话 用户名 密码
 */

class Controller_main extends MyApp {


	/** 首页 **/
	public function actionIndex()
	{

	}
	//**-------------------------------------- 报修 --------------------------------**//
	/** 报修列表 **/
	public function actionApplylist ()
	{
		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` WHERE user_id={$this->user['user_id']}");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` WHERE user_id={$this->user['user_id']} ORDER BY main_id DESC ");
		foreach ($main_lists as $key =>$value){
			// 提交者
			$main_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
		}

//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->display('main/m.apply.list.tpl.php');

	}
	/** 报修添加 **/
	public function actionApplyAdd()
	{
		if ($_POST){

			$main_place    			= $this->post->main_place;
			$main_public  			= $this->post->main_public;
			$main_content    	    = $this->post->main_content;
			$main_name    			= $this->post->main_name;

			// 写入数据库
			$info = array(

			"main_place"            =>$main_place,
			"main_public"			=>$main_public,
			"main_content"          =>$main_content,
			"user_id"				=>$this->user['user_id'],
			"main_type"				=> 1,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_main", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=main.applylist");
		}

		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->display('main/m.apply.add.tpl.php');
	}
	/** 报修修改 **/
	public function actionApplyUpdate()
	{

		$main_id   = (int)$this->get->main_id;
		$main_data = $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");

		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		if ($_POST){

			$main_place    			= $this->post->main_place;
			$main_public  			= $this->post->main_public;
			$main_content    	    = $this->post->main_content;

			// 写入数据库
			$info = array(

			"main_place"            =>$main_place,
			"main_public"			=>$main_public,
			"main_content"          =>$main_content,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_main", $info, "main_id='{$main_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=main.applylist");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->display('main/m.apply.update.tpl.php');
	}
	/** 报修查看 **/
	public function actionApplyDetails()
	{

		$main_id   = (int)$this->get->main_id;
		$main_data = $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");

		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->display('main/m.apply.details.tpl.php');
	}
	/** 删除维修部门资料 **/
	public function actionApplyDel()
	{
		$main_id = $this->get->main_id;
		$this->db->query("DELETE  FROM `tbl_main` WHERE main_id='{$main_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=main.applylist");
	}
	//**-------------------------------- 派单 ------------------------------**//
	/** 报修列表 **/
	public function actionAssignlist ()
	{
		// 分页
//		YOU::loadLib('page', "", '0');
//
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` WHERE main_type in('1','2') ");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` WHERE main_type in('1','2') ORDER BY main_type ASC ");
		foreach($main_lists as $key=>$value){
			// 操作状态
			$main_lists[$key]['main_type'] =$this->main_type[$value['main_type']];
			// 维修人员
			if(!empty($value['maince_id'])){

				$maince_name	= $this->db->fetchRow("SELECT `maince_name` FROM `tbl_maince` WHERE maince_id = {$value['maince_id']}");
				$main_lists[$key]['maince_name'] =$maince_name['maince_name'];

			}
			// 提交者
			$main_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}


//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->display('main/m.assign.list.tpl.php');

	}
	/** 报修修改 **/
	public function actionAssignUpdate()
	{

		$main_id		= (int)$this->get->main_id;
		$main_data		= $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");
		// 维修人员
		$maince_lists	= $this->db->fetchAll("SELECT * FROM `tbl_maince`");

		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		if ($_POST){

			$maince_id  			= $this->post->maince_id;
			$main_urgent    	    = $this->post->main_urgent;
			$main_remark    		= $this->post->main_remark;
			$assign_time    		= $this->post->assign_time;

			// 写入数据库
			$info = array(

			"maince_id"				=>$maince_id,
			"main_urgent"			=>$main_urgent,
			"main_remark"			=>$main_remark,
			"assign_time"			=>$assign_time,
			"is_show"				=> 2,
			"main_type"				=> 2,
			"status"                => 1,
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_main", $info, "main_id='{$main_id}'");

			// 跳转
			Helper_Client::location("派单成功", "index.php?a=main.assignlist");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->assign("maince_lists", $maince_lists);
		$this->tpl->display('main/m.assign.update.tpl.php');
	}
	//**---------------------------------- 报修 ------------------------------------**//
	/** 报修列表 **/
	public function actionMaintainList()
	{
		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` WHERE main_type in('2','3') ");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` WHERE main_type in('2','3') ORDER BY main_id DESC ");
		foreach($main_lists as $key=>$value){
			// 维修人员
			if(!empty($value['maince_id'])){

				$maince_name	= $this->db->fetchRow("SELECT `maince_name` FROM `tbl_maince` WHERE maince_id = {$value['maince_id']}");
				$main_lists[$key]['maince_name'] =$maince_name['maince_name'];
			}
			// 提交者
			$main_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}

//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->display('main/m.maintain.list.tpl.php');

	}
	/** 报修录入 **/
	public function actionMaintainUpdate()
	{

		$main_id		= (int)$this->get->main_id;
		$main_data		= $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");
		// 维修人员
		$maince_lists	= $this->db->fetchAll("SELECT * FROM `tbl_maince`");

		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		if ($_POST){

			$main_report  			= $this->post->main_report;
			$maintain_time    	    = $this->post->maintain_time;

			// 写入数据库
			$info = array(

			"main_report"			=>$main_report,
			"maintain_time"			=>$maintain_time,
			"is_show"				=> 2,
			"main_type"				=> 3,
			"status"                => 1,
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_main", $info, "main_id='{$main_id}'");

			// 跳转
			Helper_Client::location("提交成功", "index.php?a=main.maintainlist");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->assign("maince_lists", $maince_lists);
		$this->tpl->display('main/m.maintain.update.tpl.php');
	}
	/** 报修查看 **/
	public function actionMaintaindetails()
	{
		$main_id		= (int)$this->get->main_id;
		$is_show		= (int)$this->get->is_show;
		$main_data		= $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");
		// 维修人员
		$maince_name= $this->db->fetchRow("SELECT * FROM `tbl_maince` WHERE maince_id = {$main_data['maince_id']}");
		$main_data['maince_name'] = $maince_name['maince_name'];
		// 结算
		$main_data['is_show']=$this->is_show[$main_data['is_show']];
		// 提交人
		$main_data['user_name'] = $this->model->user->getNameById($main_data['user_id']);
		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->assign("is_show", $is_show);
		$this->tpl->display('main/m.maintain.details.tpl.php');

	}
	//**---------------------------- 结算 -------------------------------**//
	/** 结算列表 **/
	public function actionAccountsList()
	{
		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` WHERE main_type='3' ");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` WHERE main_type='3' ORDER BY main_id DESC ");
		foreach($main_lists as $key=>$value){
			// 公司名称
			if(!empty($value['maince_id'])){

				$maince_com	= $this->db->fetchRow("SELECT `maince_com` FROM `tbl_maince` WHERE maince_id = {$value['maince_id']}");
				$main_lists[$key]['maince_com'] =$maince_com['maince_com'];
			}
			// 结算
			$main_lists[$key]['is_show']=$this->is_show[$value['is_show']];

		}

//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->display('main/m.accounts.list.tpl.php');

	}

	/** 结算 **/
	public function actionAccountsUpdate()
	{

		$main_id		= (int)$this->get->main_id;
		$main_data		= $this->db->fetchRow("SELECT * FROM `tbl_main` WHERE `main_id`='{$main_id}'");

		if (empty($main_data)){
			Helper_Client::alert("报修内容不存在");
		}

		if ($_POST){
			$is_show  			= $this->post->is_show;

			// 写入数据库
			$info = array(

			"is_show"				=>$is_show,
			"main_type"				=> 3,
			"status"                => 1,
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_main", $info, "main_id='{$main_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=main.accountslist");
		}

		$this->tpl->assign("main_data", $main_data);
		$this->tpl->display('main/m.accounts.update.tpl.php');
	}
	//**---------------------------- 查询 ----------------------------**//
	/** 查询 **/
	public function actionDemandList()
	{
		// 结算类型
		$is_shows = $this->is_show;

		// 维修单位
		$maince_lists		= $this->db->fetchAll("SELECT * FROM `tbl_maince`");
		// 报修人
		$main_names			= $this->db->fetchAll("SELECT distinct user_id FROM `tbl_main`");
		foreach ($main_names as $key=>$value){
			// 名字
			$main_names[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}
		// 查询条件
		$main_name		= $this->get->main_name;
		$maince_com		= $this->get->maince_com;
		$from			= $this->get->from;
		$to				= $this->get->to;
		$is_show		= $this->get->is_show;

		$where = " ";

		if(!empty($main_name)){
			$staff_id = $this->model->user->getIdByName($main_name);
		
			// 申报人姓名
			$where .= " AND a.user_id LIKE '{$staff_id}'";
			$this->tpl->assign("main_name", $main_name);

		}
		if(!empty($maince_com)){
			// 维修单位
			$where .= " AND b.maince_com LIKE '%$maince_com%'";
			$this->tpl->assign("maince_com", $maince_com);
		}
		if(!empty($from)){
			// 维修完成开始时间
			$where .= " AND a.maintain_time >= '$from' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			// 维修完成结束时间
			$where .= " AND a.maintain_time <= '$to'";
			$this->tpl->assign("to", $to);
		}
		if(!empty($is_show)){
			// 维修完成结束时间
			$where .= " AND a.is_show = '$is_show'";
			$this->tpl->assign("is_show", $is_show);
		}


//		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` AS a,`tbl_maince` AS b  WHERE a.maince_id= b.maince_id AND main_type='3' {$where}");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` AS a,`tbl_maince` AS b  WHERE a.maince_id= b.maince_id {$where} AND main_type='3'  ORDER BY a.main_id DESC ");

		foreach($main_lists as $key=>$value){
			// 公司名称
			if(!empty($value['maince_id'])){

				$maince_com	= $this->db->fetchRow("SELECT `maince_com` FROM `tbl_maince` WHERE maince_id = {$value['maince_id']}");
				$main_lists[$key]['maince_com'] =$maince_com['maince_com'];
			}
			// 结算
			$main_lists[$key]['is_show']=$this->is_show[$value['is_show']];
			// 提交者
			$main_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
		}

//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->assign("maince_lists", $maince_lists);
		$this->tpl->assign("is_shows", $is_shows);

		$this->tpl->assign("main_names", $main_names);
		$this->tpl->display('main/m.demand.list.tpl.php');

	}

}

?>